<template>
  <view class="edior">
    <TopBar title="车辆管理" />
    <!-- 暂未添加车辆 -->
    <view class="noAddPlate" v-if="total === 0">


      <view class="box">
        <view class="content">
          <image src="../../images/Frame.png" mode="aspectFill">11</image>
        </view>
        <text>尚未绑定车辆</text>
      </view>


      <view class="box">
        <view class="content">
          <image src="../../images/Frame.png" mode="aspectFill"></image>
        </view>
        <text>尚未绑定车辆</text>
      </view>
      <view class="footer">
        <text>本月还可添加3次</text>
        <navigator url="/pages/addCar/addCar">
          <button type="default">+ 添加车辆</button>
        </navigator>
      </view>
    </view>
    <!-- 已经添加车辆 -->
    <view class="existPlate" v-else>
      <view class="box" v-for="item in list" :key="item.id">
        <view class="info">{{ item.carNo }}</view>
        <button type="default" @click="deletePlate(item.id)">解绑</button>
      </view>
      <navigator class="add" url="/pages/addCar/addCar">
        <text class="icon">+</text>
        <text class="name">添加车辆</text>
      </navigator>
      <text class="footer">本月还可添加{{ 5 - total }}次</text>
    </view>
  </view>
</template>
<script setup>
import { ref, reactive } from "vue";
import { apiPostCarList,apiPostCarDelete } from "@/api/car";
import TopBar from "../../component/topbar.vue";
import { onShow } from "@dcloudio/uni-app";
const token = uni.getStorageSync("token");

console.log("BASE_URL:", import.meta.env.VITE_API_BASE_URL);
//判断是否登录，没有就跳转登录页面
if (token === "") {
  uni.reLaunch({
    url: "/pages/login/login",
  });
}
const total = ref(0);
const list = ref([]);
//车辆列表
// const loadCarList  = ()=>{
// uni.request({
//   url: "https://qwscrm.com:9202/car/v1/query-list",
//   method: "POST",
//   header: { Authorization: `Bearer ${token}` },
//   data: {
//     curPage: 1,
//     pageSize: 10,
//   },
//   success: (res) => {
//     console.log("车辆列表："+res);
//     list.value = res.data.data.list;
//     total.value = res.data.data.total;

//   },
// });
// }
onShow(()=>{
loadCarList();
})
const loadCarList = async () => {
  console.log(11);
  const res = await apiPostCarList({});
  list.value = res.data.list;
  total.value = res.data.total;
};

//解绑车辆
const deletePlate =  (id) => {
  uni.showModal({
    title: "确认解绑",
    content: "确定要解绑这辆车吗？",
    success:async (res) => {
      //确认删除就调用接口,删除根据传递的参数id来删除
      if (res.confirm) {
        // uni.request({
        //   url: `https://qwscrm.com:9202/car/v1/delete/${cardId}`,
        //   method: "DELETE",
        //   header: { Authorization: `Bearer ${token}` },
        //   success: (res) => {
        //     if (res.statusCode === 200) {
        //       loadCarList(); // ✅ 关键：刷新列表
        //       uni.showToast({ title: "解绑成功", icon: "none" });
        //     } else {
        //       uni.showToast({ title: "解绑失败", icon: "none" });
        //     }
        //   },
        // });
        const res =await apiPostCarDelete(id)
        console.log(res);
        if (res.code === 0) {
              loadCarList(); // ✅ 关键：刷新列表
              uni.showToast({ title: "解绑成功", icon: "none" });
            } else {
              uni.showToast({ title: "解绑失败", icon: "none" });
            }
      }
    },
  });
};
</script>
<style scoped lang="less">
.edior {
  .noAddPlate {
    .box {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin-top: 212rpx;
      .content {
        border: 1px dashed #333;
        width: 128rpx;
        height: 128rpx;
        margin-bottom: 20rpx;

        image {
          width: 117rpx;
          height: 109rpx;
          padding: 10rpx 8rpx 6rpx 6rpx;
        }
      }
      text {
        width: 168rpx;
        height: 28rpx;
        font-family: "Source Han Sans CN, Source Han Sans CN";
        font-weight: 500;
        font-size: 28rpx;
        color: #b0b0b0;
        line-height: 28rpx;
        text-align: left;
        font-style: normal;
        text-transform: none;
      }
    }
    .footer {
      margin-top: 212rpx;
      text {
        width: 212rpx;
        height: 28rpx;
        font-family: "Source Han Sans CN, Source Han Sans CN";
        font-weight: 400;
        font-size: 28rpx;
        color: #999999;
        line-height: 28rpx;
        text-align: left;
        font-style: normal;
        margin-left: 270rpx;
      }
      button {
        width: 686rpx;
        height: 96rpx;
        background: #0680fe;
        border-radius: 100rpx;
        font-weight: bold;
        font-size: 36rpx;
        color: #ffffff;
        line-height: 96rpx;
        text-align: center;
        margin-top: 16rpx;
      }
    }
  }
  .existPlate {
    .box {
      width: 686rpx;
      height: 112rpx;
      background: #ffffff;
      border-radius: 4rpx;
      border: 2rpx solid #e7e7e7;
      margin: 32rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .info {
        margin-left: 48rpx;
      }

      button {
        width: 120rpx;
        height: 48rpx;
        background: #ffffff;
        border-radius: 48rpx;
        border: 2rpx solid #e7e7e7;
        font-size: 22rpx;
        color: #b7b6b6;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 32rpx;
      }
    }

    .add {
      margin-top: 32rpx;
      color: #0680fe;
      font-size: 32rpx;
      display: flex;
      align-items: center;
      margin-left: 284rpx;
      .icon {
        width: 48rpx;
        height: 48rpx;
        border: 2rpx dashed #333;
        /* 统一用 rpx，更适配 */
        border-radius: 8rpx;
        /* 可选：让边角圆润一点 */
        font-weight: bold;
        /* 加粗 + 号 */
        font-size: 40rpx;
        /* 让 + 更大更明显 */
        line-height: 1;
        /* 避免字体上下间距影响居中 */

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .name {
        margin-left: 8rpx;
      }
    }

    .footer {
      width: 182rpx;
      height: 24rpx;
      font-family: "Source Han Sans CN, Source Han Sans CN";
      font-weight: 400;
      font-size: 24rpx;
      color: #999999;
      line-height: 24rpx;
      text-align: left;
      font-style: normal;
      text-transform: none;
      margin-left: 284rpx;
    }
  }
}
</style>
